package org.springcloud.wisdom.springcloud_03_freshman.common.controller;



import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springcloud.wisdom.springcloud_03_freshman.common.bean.ResponseData;
import org.springcloud.wisdom.springcloud_03_freshman.common.util.FileUtil;
import org.springcloud.wisdom.springcloud_03_freshman.index.login.dao.Clogindao;
import org.springcloud.wisdom.springcloud_03_freshman.index.login.entity.Mall;
import org.springcloud.wisdom.springcloud_03_freshman.index.login.service.CmallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
//商家注册（有上传运营执照）
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/file")
public class FileController {
    @Value("${myfile.user.photopath}")
    private String savePath;
    @Autowired
    private Clogindao clogindao;
    @Autowired
    private CmallService cmallService;
    @PostMapping("/addWithPhoto")
    public ResponseData<String> addWithPhoto(@RequestParam("userphoto") MultipartFile file,
                                             @RequestParam("mname") String mname,
                                             @RequestParam("mpassword") String mpassword,
                                             @RequestParam("mphone") String mphone,
                                             @RequestParam("canteen") String canteen,
                                             HttpServletRequest request) {
        boolean result = false;
        String fileUrl = null;
        if (!file.isEmpty()) {
            String newName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) +
                    FileUtil.getExtName(file.getOriginalFilename());
            // 保存文件,得到文件保存后的url地址
            fileUrl = FileUtil.uploadFile(file, newName, savePath, request);
            if (fileUrl != null) {
                result = true;
            }
            // 检查用户名是否重复
            if (cmallService.checkMnameExists(mname)) {
                return new ResponseData<>(601, "用户名已存在", null);
            }
            // 保存到数据库
            Mall mall = new Mall();
            mall.setMname(mname);
            mall.setMpassword(mpassword);
            mall.setMphone(mphone);
            mall.setCanteen(canteen);
            mall.setMlicense(fileUrl);
            result &= clogindao.addMall(mall) > 0;
        }
        log.error("File upload result: " + result + ", URL: " + fileUrl);
        ResponseData<String> responseData = new ResponseData<>();
        responseData.setData(fileUrl);
        if (result) {
            responseData.setErrorCode(200);
            responseData.setMsg("成功");
        } else {
            responseData.setErrorCode(605);
            responseData.setMsg("失败");
        }
        return responseData;
    }
}
